class Solution {
public:
    int findLength(vector<int>& nums1, vector<int>& nums2) {
 vector<vector<int> > dp(nums1.size(),vector<int>(nums2.size(),0));
        int max = 0;
        if(nums1[0] == nums2[0]) dp[0][0] = 1;
        for(int j = 1;j < nums2.size();j++){
            if(nums2[j] == nums1[0]){
                dp[0][j] = 1;
                max = 1;
            }else{
                dp[0][j] = dp[0][j - 1];
            }
        }
        for(int i = 1;i < nums1.size();i++){
            if(nums1[i] == nums2[0]){
                dp[i][0] = 1;
                max = 1;
            }else{
                dp[i][0] = dp[i - 1][0];
            }
        }
       
        for(int i = 1;i < nums1.size();i++){
            for(int j = 1;j < nums2.size();j++){
                if(nums1[i] == nums2[j]){
                    if(nums1[i - 1] == nums2[j - 1]){
                        dp[i][j] = dp[i - 1][j - 1] + 1;
                    }else{
                        dp[i][j] = 1;
                    }
                    if(dp[i][j] > max){
                        max = dp[i][j];
                    }
                }
            }
        }
        
        return max;
    }
};
